package com.atqar.block;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockqueueDemo {
    public static void main(String[] args) throws InterruptedException {
        //参数是容量大小
        ArrayBlockingQueue<String> block= new ArrayBlockingQueue<String>(3);
       /* //add方法添加成功true超出容量大小会抛出异常java.lang.IllegalStateException
        System.out.println(block.add("a"));
        System.out.println(block.add("b"));
        System.out.println(block.add("c"));
        //System.out.println(block.add("d"));

        //element放回队列的头元素
        System.out.println(block.element());

        //remove删除并返回删除的元素超出容量大小也会抛出异常java.util.NoSuchElementException
        System.out.println(block.remove());
        System.out.println(block.remove());
        System.out.println(block.remove());
        //System.out.println(block.remove());*/

/*        //offer添加元素方法成功true超出false
        System.out.println(block.offer("a"));
        System.out.println(block.offer("b"));
        System.out.println(block.offer("c"));
        //System.out.println(block.offer("d"));

        //element放回队列的头元素
        System.out.println(block.peek());

        //取出队列的数据没有的的就返回null
        System.out.println(block.poll());
        System.out.println(block.poll());
        System.out.println(block.poll());
        System.out.println(block.poll());
       */


  /*      //当队列满了在put添加元素是会一直等待数据被移除或超时
        block.put("a");
        block.put("b");
        block.put("c");
        System.out.println(block.size());
        //block.put("d");
        //System.out.println(block.size());

        //take取出队列的数据如果超出队列的数据线程会等待添加数据后在取出或者一直等待
        System.out.println(block.take());
        System.out.println(block.take());
        System.out.println(block.take());
        System.out.println(block.take());
        System.out.println(block.size());

*/

        //参数是添加的队列,超时时间,超时时间单位,当超出容量大小会等待,超出等待时间线程就会结束
        System.out.println(block.offer("a", 1, TimeUnit.SECONDS));
        System.out.println(block.offer("b", 1, TimeUnit.SECONDS));
        System.out.println(block.offer("c", 1, TimeUnit.SECONDS));
        //System.out.println(block.offer("d", 1, TimeUnit.SECONDS));

        //参数是超时时间,超时时间单位,当取出队列超出容量大小会等待,超出等待时间线程就会结束
        System.out.println(block.poll(1, TimeUnit.SECONDS));
        System.out.println(block.poll(1, TimeUnit.SECONDS));
        System.out.println(block.poll(1, TimeUnit.SECONDS));
        System.out.println(block.poll(1, TimeUnit.SECONDS));

    }
}
